/*
 * dbm_policy_handler.c
 *
 *  Created on: 2012-12-14
 *      Author: macro
 */

#include "pdb/dbm_policy_handler.h"
#include "cmn/ptypes.h"
#include "pdb/policy_parse.h"
#include "pdb/public_include.h"
#include "pdb/policy_table.h"


int dbm_pdb_handle(u8* pkt,int length){
	printf("Receive %d data\n",length);
	pdb_packet* pdbpkt = (pdb_packet*)malloc(sizeof(pdb_packet)+length);
	memcpy(pdbpkt,pkt,length+sizeof(pdb_packet));
	if(pdbpkt->action == UPDATE_PDB){
		printf("this is a update pkt\n");
		dbm_handle_policy_update(pdbpkt->data);
	}
	else if(pdbpkt->action == REQUEST_POLICY){
		printf("this is a request pkt\n");
		dbm_handle_policy_request(pdbpkt->data);
	}
	else{
		printf("Pkt can't be identify\n");
		return -1;
	}
}
int dbm_handle_policy_request(u8* pkt){
	int key;
	memcpy(&key,pkt,sizeof(int));
	struct _policy_element* policy;
	int No = find_policy_table(g_pt,key);
	printf("xxxxxxxxxxxxx%dxxxxxxxxxxxxxxx",key);
	if(No>=0&&No<g_pt->used_len){
		policy = &g_pt->p_element[No];
		send_policy(policy);
	}
	else
	{
		printf("Can't found policy!!!!!\n");
	}
}

int dbm_handle_policy_update(u8* pkt){
	process_policy(pkt,0);//this function not need p2;
}
